package com.cus.tom.transfer.adapter;

import android.Manifest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cus.tom.transfer.App;
import com.cus.tom.transfer.bean.FileType;
import com.cus.tom.transfer.bean.MediaData;
import com.cus.tom.transfer.databinding.InfoItemBinding;
import com.cus.tom.transfer.frag.RecordInfoFragment;
import com.cus.tom.transfer.utils.Utils;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.io.File;

public class InfoAdapter extends BatchAdapter {

    boolean[] right = new boolean[3];

    public InfoAdapter(RecordInfoFragment act, int type) {
        super(act, type);
        right[0] = XXPermissions.isGranted(App.getApp(), Permission.WRITE_CONTACTS);
        right[1] = XXPermissions.isGranted(App.getApp(), Permission.READ_SMS);
        right[2] = XXPermissions.isGranted(App.getApp(), Permission.WRITE_CALL_LOG);
    }

    public void updateRight(int i, boolean bo) {
        right[i] = bo;
        notifyItemRangeChanged(0, getItemCount());
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 3) {
            InfoItemBinding binding = InfoItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new InfoHolder(binding);
        } else {
            return super.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof InfoHolder) {
            ((InfoHolder) holder).bind(position);
        } else {
            super.onBindViewHolder(holder, position);
        }
    }

    class InfoHolder extends ImgHolder {
        InfoItemBinding binding;

        InfoHolder(InfoItemBinding binding) {
            super(binding);
            this.binding = binding;
            if (type == 1) {
                binding.btn.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        protected void setImg(MediaData data) {

        }

        @Override
        protected void bind(int p) {
            super.bind(p, binding.check, itemView);
            MediaData item = (MediaData) list.get(p);
            File f = new File(item.getPath());
            binding.size.setText(Utils.formatSize(f.length()));
            FileType type = FileType.valueOf(item.getType());
            switch (type) {
                case CONTRACT:
                    binding.title.setText("联系人");
                    binding.btn.setText(right[0] ? "导入" : "去授权");
                    binding.btn.setOnClickListener(v -> {
                        ((RecordInfoFragment) act).checkPermission(item, Permission.WRITE_CONTACTS);
                    });
                    break;
                case SMS:
                    binding.title.setText("短信");
                    binding.btn.setText(right[1] ? "导入" : "去授权");
                    binding.btn.setOnClickListener(v -> {
                        String[] ss = new String[]{Manifest.permission.READ_SMS,
                                Manifest.permission.SEND_SMS,
                                Manifest.permission.RECEIVE_SMS};

                        ((RecordInfoFragment) act).checkPermission(item, ss);
                    });
                    break;
                case CALL:
                    binding.title.setText("通话记录");
                    binding.btn.setText(right[2] ? "导入" : "去授权");
                    binding.btn.setOnClickListener(v -> {
                        ((RecordInfoFragment) act).checkPermission(item, Permission.WRITE_CALL_LOG);
                    });
                    break;
            }
        }
    }
}
